package com.shopowner.dao.impl;

import com.shopowner.dao.FieldDao;
import com.shopowner.entity.Field;
import com.shopowner.mapper.FieldMapper;
import com.shopowner.page.PageInfo;
import com.shopowner.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("fieldDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class FieldDaoImpl implements FieldDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private FieldMapper fieldMapper;
    public List<Field>  getAllFields() throws Exception{
        return fieldMapper.selectByExample(null);
    }
    public Field  selectFieldByFno(String fno) throws Exception{
        return fieldMapper.selectByPrimaryKey(fno);
    }
    @Override
    public Integer deleteFieldByFno(String fno)  throws Exception{
        return fieldMapper.deleteByPrimaryKey(fno);
    }

    @Override
    public Integer updateFieldByFno(Field field)  throws Exception{
        return fieldMapper.updateByPrimaryKey(field);
    }

    @Override
    public Integer addField(Field field)  throws Exception{
        field.setFno(initId());
        return fieldMapper.insertSelective(field);
    }
    public String initId(){
        String maxFno = fieldMapper.getMaxFno();
        return GenNumTools.initId("c",4,maxFno);
    }

    @Override
    public List<Field> getFieldByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)fieldMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return fieldMapper.getFieldByPage(map);
    }

//
//    @Override
//    public Field selectFieldWithChildren(String fno){
//        return fieldMapper.selectFieldWithChildren(fno);
//    }
//
//    @Override
//    public Field getFieldWithOrdrees(String fno) throws Exception {
//        return fieldMapper.getFieldWithOrdrees(fno);
//    }
//
//    @Override
//    public Field getAccountByAccountName(String cname) {
//            return fieldMapper.getAccountByAccountName(cname);
//    }
//
//    /**
//     *
//     * @param cname
//     * @return
//     */
//    public List<Field> getFieldByExample(String cname) {
//        FieldExample fieldExample =new FieldExample();
//        fieldExample.createCriteria().andCnameEqualTo(cname);
//        return fieldMapper.selectByExample(fieldExample);
//    }
//
//    @Override
//    public Integer updateField(Field field) { return fieldMapper.updateByPrimaryKey(field); }

//    public Integer updateByPrimaryKeySelective(Field field) { return fieldMapper.updateByPrimaryKeySelective(field); }
//
//
//    @Override
//    public Integer updateFields(Field field) { return fieldMapper.updateByPrimaryKeys(field); }

}
